import 'dart:io';

void main() {
  List<Map<String, double>> incomeList = [];
  List<Map<String, double>> expenseList = [];

  while (true) {
    print('\n===== Budget Management Application =====');
    print('1. Add Income');
    print('2. Add Expense');
    print('3. View Summary');
    print('4. Exit');
    stdout.write('Enter your choice: ');
    String? choice = stdin.readLineSync();

    if (choice == '4') {
      print('Exiting... Goodbye!');
      break;
    }

    switch (choice) {
      case '1':
        stdout.write('Enter income description: ');
        String? desc = stdin.readLineSync();
        stdout.write('Enter amount: ');
        double? amount = double.tryParse(stdin.readLineSync() ?? '');
        if (amount != null && amount > 0) {
          incomeList.add({desc ?? 'Income': amount});
          print('Income added successfully!');
        } else {
          print('Invalid amount! Must be a positive number.');
        }
        break;

      case '2':
        stdout.write('Enter expense description: ');
        String? desc = stdin.readLineSync();
        stdout.write('Enter amount: ');
        double? amount = double.tryParse(stdin.readLineSync() ?? '');
        if (amount != null && amount > 0) {
          expenseList.add({desc ?? 'Expense': amount});
          print('Expense added successfully!');
        } else {
          print('Invalid amount! Must be a positive number.');
        }
        break;

      case '3':
        double totalIncome = 0;
        double totalExpense = 0;

        for (var item in incomeList) {
          totalIncome += item.values.first;
        }
        for (var item in expenseList) {
          totalExpense += item.values.first;
        }

        double net = totalIncome - totalExpense;
        String result = net >= 0 ? 'Net Profit' : 'Net Loss';

        print('\n--- Budget Summary ---');
        print('Total Income: \$${totalIncome.toStringAsFixed(2)}');
        print('Total Expenses: \$${totalExpense.toStringAsFixed(2)}');
        print('$result: \$${net.abs().toStringAsFixed(2)}');

        // Optional tax calculation
        double tax = net > 0 ? net * 0.15 : 0;
        if (tax > 0) {
          print('Tax to be paid (15%): \$${tax.toStringAsFixed(2)}');
        }
        break;

      default:
        print('Invalid choice! Please try again.');
    }
  }
}
